package com.spzx.product.controller;

import com.github.pagehelper.PageHelper;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.log.annotation.Log;
import com.spzx.common.security.annotation.InnerAuth;
import com.spzx.product.domain.*;
import com.spzx.product.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 商品Controller
 */
@Tag(name = "商品接口管理")
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {

	@Autowired
	private ProductService productService;

	@Operation(summary = "获取销量好的sku")
	@GetMapping("getTopSale")
	public R<List<ProductSku>> getTopSale() {
		return R.ok(productService.getTopSale());
	}

	// 添加
	@PostMapping("/add")
	public AjaxResult add(@RequestBody Product product) {
		return toAjax(productService.insertProduct(product));
	}

	/**
	 * 查询商品列表
	 */
	@Operation(summary = "查询商品列表")
	@GetMapping("/list")
	public TableDataInfo list(Product product) {
		startPage();
		List<Product> list = productService.selectProductList(product);
		return getDataTable(list);
	}

	/**
	 * 获取商品详细信息
	 */
	@Operation(summary = "获取商品详细信息")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return success(productService.selectProductById(id));
	}

	/**
	 * 修改商品
	 *
	 * @param product
	 * @return
	 */
	@Operation(summary = "修改商品")
	@PutMapping
	public AjaxResult edit(@RequestBody Product product) {
		return toAjax(productService.updateProduct(product));
	}

	/**
	 * 删除商品
	 *
	 * @param ids
	 * @return
	 */
	@Operation(summary = "删除商品")
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(productService.deleteProductByIds(ids));
	}

	// 远程调用使用，商品列表
	@GetMapping("/skuList/{pageNum}/{pageSize}")
	@Operation(summary = "删除商品")

	public R<TableDataInfo> skuList(@PathVariable Integer pageNum,
									@PathVariable Integer pageSize,
									@ModelAttribute SkuQuery skuQuery) {
		// 设置分页参数
		PageHelper.startPage(pageNum, pageSize);
		// 调用service方法根据条件查询
		List<ProductSku> list = productService.selectProductSkuList(skuQuery);

		TableDataInfo dataTable = getDataTable(list);
		return R.ok(dataTable);
	}

	//////////////////////////////////////////////
	///////////////////远程调用接口////////////////
	/////////////////////////////////////////////
	// @GetMapping(value = "/getProductSku/{skuId}")


	// 获得商品sku信息
	@GetMapping("/getProductSku/{skuId}")
	@Operation(summary = "获得商品sku信息")
	@InnerAuth
	public R<ProductSku> getProductSku(@PathVariable("skuId") Long skuId) {
		// 调用service方法根据skuId查询商品sku信息
		return R.ok(productService.getProductSkuV2(skuId));
	}

	// 获得商品信息
	@GetMapping("/getProduct/{productId}")
	@Operation(summary = "获得商品信息")
	@InnerAuth
	public R<Product> getProductById(@PathVariable("productId") Long productId) {
		// 调用service方法根据productId查询商品信息
		return R.ok(productService.selectProductById(productId));
	}

	// 获得sku的最新价格
	@GetMapping("/getSkuPrice/{skuId}")
	@Operation(summary = "获得sku的最新价格")
	@InnerAuth
	public R<SkuPrice> getNewPrice(@PathVariable("skuId") Long skuId) {
		// 调用service方法根据skuId查询商品最新价格
		return R.ok(productService.getSkuPrice(skuId));
	}

	// 获取商品详情数据
	@GetMapping("/getProductDetails/{productId}")
	@Operation(summary = "获取商品详情数据")
	@InnerAuth
	public R<ProductDetails> getDetails(@PathVariable("productId") Long productId) {
		return R.ok(productService.getDetails(productId));
	}

	// 获取商品规格数据
	@GetMapping("/getSkuSpecValue/{productId}")
	@Operation(summary = "获取商品下的Sku规格数据")
	@InnerAuth
	public R<Map<String, Long>> getSpec(@PathVariable("productId") Long productId) {
		return R.ok(productService.getSkuSpecValue(productId));
	}

	// 获得sku的库存数量
	@GetMapping("/getSkuStock/{skuId}")
	@InnerAuth
	@Operation(summary = "获得sku的库存数量")
	public R<SkuStockVo> getSkuStock(@PathVariable("skuId") Long skuId) {
		// 调用service方法根据skuId查询商品库存数量
		return R.ok(productService.getSkuStock(skuId));
	}

	@Operation(summary = "批量查询sku最新价格")
	@InnerAuth
	@GetMapping(value = "/getSkuPriceList")
	R<List<SkuPrice>> getSkuPriceList(@RequestParam("skuIds") List<Long> skuIds) {
		return R.ok(productService.getSkuPriceList(skuIds));
	}

	@InnerAuth
	@Operation(summary = "检查与锁定库存")
	@PostMapping("checkAndLock/{orderNo}")
	public R<String> checkAndLock(@PathVariable String orderNo,
								  @RequestBody List<SkuLockVo> skuLockVoList) {
		try {
			return R.ok(productService.checkAndLock(orderNo, skuLockVoList));
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.ok(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			return R.ok("库存不足");
		}
	}
}
